/**
 * @file       godemool/sprop.cc
 * @brief      sent property class.
 * @author     ben "boin" bonoeil
 * @date       2015-02
 */

/* Copyright (c) 2007-2015, ben "boin" bonoeil

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above
   copyright notice, this list of conditions and the following
   disclaimer in the documentation and/or other materials provided
   with the distribution.
   * Neither the name of the ben "boin" bonoeil nor the names of its
   contributors may be used to endorse or promote products derived
   from this software without specific prior written permission.

   NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
   GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY
   ben "boin" bonoeil ''AS IS'' AND ANY EXPRESS OR IMPLIED
   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   DISCLAIMED. IN NO EVENT SHALL ben "boin" bonoeil BE LIABLE FOR ANY
   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "godemool/sprop.h"
#include "godemool/bitstream.h"
#include "godemool/csgo.h"

#if defined(HAVE_SCALBN) && defined(HAVE_MATH_H)
#include <cmath>
#endif

using namespace boin;


SProp::Value::Value()
{
  vec.x = vec.y = vec.z = 0; // Clear the largest union member
}

SProp::Value::Value(const std::string & val)
{
  str = StringPool::pool[val];
}


class SPropDecoder : public SProp, public BitStream
{
  
  enum {
    COORD_INTEGER_BITS       = 14,
    COORD_FRACTIONAL_BITS    = 5,
    COORD_INTEGER_BITS_MP    = 11,
    COORD_FRACTIONAL_BITS_MP = 3,
    NORMAL_FRACTIONAL_BITS   = 11,
  };
  
  enum EBitCoordType {
    None, LowPrecision, Integral
  };
  
  float ReadBitAngle(unsigned int nbits)
  {
    assert(nbits < 32);
    static const float scale[32] = {
      360.0f/(1<< 0), 360.0f/(1<< 1), 360.0f/(1<< 2), 360.0f/(1<< 3),
      360.0f/(1<< 4), 360.0f/(1<< 5), 360.0f/(1<< 6), 360.0f/(1<< 7),
      360.0f/(1<< 8), 360.0f/(1<< 9), 360.0f/(1<<10), 360.0f/(1<<11),
      360.0f/(1<<12), 360.0f/(1<<13), 360.0f/(1<<14), 360.0f/(1<<15),
      360.0f/(1<<16), 360.0f/(1<<17), 360.0f/(1<<18), 360.0f/(1<<19),
      360.0f/(1<<20), 360.0f/(1<<21), 360.0f/(1<<22), 360.0f/(1<<23),
      360.0f/(1<<24), 360.0f/(1<<25), 360.0f/(1<<26), 360.0f/(1<<27),
      360.0f/(1<<28), 360.0f/(1<<29), 360.0f/(1<<30), 360.0f/(1<<31)
    };
    return scale[nbits] * float(Read(nbits));
  }
  
  inline float ReadI(int bits) { return float(Read(bits)+1); }
  
  inline float ReadF(int nbits) {
#ifdef HAVE_SCALBN
    return scalbn(Read(nbits),-nbits); // To be tested
#else
    const float scale = 1.0f / float(1 << nbits);
    return scale * float(Read(nbits));
#endif
  }
  
  inline float ReadCoordI() { return ReadI(COORD_INTEGER_BITS); }
  inline float ReadCoordMpI() { return ReadI(COORD_INTEGER_BITS_MP); }
  inline float ReadCoordF() { return ReadF(COORD_FRACTIONAL_BITS); }
  inline float ReadCoordMpF() { return ReadF(COORD_FRACTIONAL_BITS_MP); }
  
  float ReadBitCoord()
  {
    float value = 0;
    int flags = Read(2) << 1; // I(4) | F(2) | N(1)
    if (flags) {
      flags |= Read1(); // N
      if (flags & 4)
        value = ReadCoordI();
      if (flags & 2) 
        value += ReadCoordF();
    }
    if (flags & 1)
      value = -value;
    return value;
  }
  
  float ReadBitCoordMP( EBitCoordType coordType)
  {
    float value = 0;
    int flags = Read(2) << 1; // P(4) | I(2) | N(1)
    
    if (coordType == Integral) {
      if (flags & 2) {
        flags |= Read1(); // N
        value = (flags & 4) ? ReadCoordMpI() : ReadCoordI();
      }
    } else {
      flags |= Read1(); // N
      if (flags & 2)
        value = (flags & 4) ? ReadCoordMpI() : ReadCoordI();
      value += (coordType == LowPrecision) ? ReadCoordMpF() : ReadCoordF();
    }
    return (flags & 1) ? -value : value;
  }
  
  float ReadBitCellCoord(int bits, EBitCoordType coordType)
  {
    float value = float(Read(bits));
    if (coordType != Integral)
      value += coordType == LowPrecision ? ReadCoordMpF() : ReadCoordF();
    return value;
  }
  
  float ReadBitNormal(void)
  {
    const int flags = Read1();
    float value = float(Read(NORMAL_FRACTIONAL_BITS));
    value *= 1.0f / float((1<<(NORMAL_FRACTIONAL_BITS))-1);
    if (flags)
      value = -value;
    return value;
  }
  
  int32_t Integer(const CSVCMsg_SendTable::sendprop_t * sp)
  {
    const int flags = sp->flags() & (VARINT | UNSIGNED);
    int32_t v;
    
    switch (flags) {
    case UNSIGNED|VARINT: v = ReadVaU32(); break;
    case VARINT: v = ReadVaI32(); break;
    case UNSIGNED: v = Read(sp->num_bits()); break;
    case 0: v = Extend(sp->num_bits()); break;
    default: v= 0; assert(!"How on earth ?");
    }
    return v;
  }
  
  inline float ReadInterp(int nbits, const float lo, const float hi) {
    const float value = float(Read(nbits)) / float((1 << nbits)-1);
    return lo + (hi-lo) * value;
  }
  
  float Float(const CSVCMsg_SendTable::sendprop_t * sp)
  {
    const int flags = sp->flags();
    
    if (flags & COORD)
      return ReadBitCoord();
    
    if (flags & COORD_MP)
      return ReadBitCoordMP(None);
    
    if (flags & COORD_MP_LOWPRECISION)
      return ReadBitCoordMP(LowPrecision);
    
    if (flags & COORD_MP_INTEGRAL)
      return ReadBitCoordMP(Integral);
    
    if (flags & NOSCALE)
      return ReadFP32();
    
    if (flags & NORMAL)
      return ReadBitNormal();
    
    if (flags & CELL_COORD)
      return ReadBitCellCoord(sp->num_bits(), None);
    
    if (flags & CELL_COORD_LOWPRECISION)
      return ReadBitCellCoord(sp->num_bits(), LowPrecision);
    
    if (flags & CELL_COORD_INTEGRAL)
      return ReadBitCellCoord(sp->num_bits(), Integral);
    
    return ReadInterp(sp->num_bits(), sp->low_value(), sp->low_value());
  }
  
};
